package com.briup.web_demo2.service.impl;

import com.briup.web_demo2.enums.ResponseEnum;
import com.briup.web_demo2.exception.SystemException;
import com.briup.web_demo2.mapper.StudentMapper;
import com.briup.web_demo2.pojo.Student;
import com.briup.web_demo2.response.PageTemplate;
import com.briup.web_demo2.response.Response;
import com.briup.web_demo2.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
//标识，交给Spring容器管理
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    //新增学生
    @Override
    public void insertStudent(Student student) {
        //参数校验
        if(Objects.nonNull(student.getId())){
            throw new SystemException(ResponseEnum.INSERT_NOT_HAVA_ID);
        }
        if(Objects.isNull(student.getName())|| !StringUtils.hasText(student.getName())){
            throw new SystemException(ResponseEnum.STUDENT_NAME_IS_NOT_NULL);
        }
        if(student.getGender()!=1 && student.getGender()!=0){
            throw new SystemException(ResponseEnum.GENDER_FORMAT_ERROR);
        }
        //手动设置头像初始值
        student.setAvatar("https://api.vvhan.com/api/avatar/recommend");
        //修改入学时间
        student.setRegisterTime(LocalDateTime.now());
        //新增学生
        studentMapper.insertStudent(student);
    }

    //根据学生Id删除学生信息
    @Override
    public void deleteStudent(Integer id) {
        if(Objects.isNull(id)){
            throw new SystemException(ResponseEnum.ID_IS_NOT_NULL);
        }
        Student student=studentMapper.selectStudentById(id.toString());
        if(Objects.isNull(student)){
            throw new SystemException(ResponseEnum.STUDENT_IS_NOT_NULL);
        }
        studentMapper.deleteStudentById(id);
    }

    //根据学生Id修改学生信息
    @Override
    public void updateStudentById(Student student) {
        if(Objects.isNull(student.getId())){
            throw new SystemException(ResponseEnum.ID_IS_NOT_NULL);
        }
        //根据Id查找学生
        Student student1 = studentMapper.selectStudentById(student.getId().toString());
        if(Objects.isNull(student1)){
            throw new SystemException(ResponseEnum.STUDENT_IS_NOT_NULL);
        }
        if(Objects.isNull(student.getName())|| !StringUtils.hasText(student.getName())){

            throw new SystemException(ResponseEnum.STUDENT_NAME_IS_NOT_NULL);
        }
        if(student.getGender()!=1 && student.getGender()!=0){
            throw new SystemException(ResponseEnum.GENDER_FORMAT_ERROR);
        }
        studentMapper.updateStudentById(student);
    }

    @Override
    public Student selectStudentById(String id) {
        if(Objects.isNull(id)){
            throw new SystemException(ResponseEnum.INSERT_NOT_HAVE_ID);
        }
        Student student = studentMapper.selectStudentById(id);
        if(Objects.isNull(student.getName())|| !StringUtils.hasText(student.getName())){

            throw new SystemException(ResponseEnum.STUDENT_NAME_IS_NOT_NULL);
        }
        return student;
    }
    //手动分页
    @Override
    public PageTemplate<Student> getBasePage(Integer pageNum, Integer pageSize) {
        //参数校验
        if(Objects.isNull(pageNum) || Objects.isNull(pageSize)){
            throw new SystemException(ResponseEnum.PAGENUM_OR_PAGESIZE_IS_NUL);
        }
        //计算页码
        pageNum=(pageNum-1)*pageSize;
        List<Student> students=studentMapper.getBasePage(pageNum,pageSize);
        Integer total=studentMapper.selectTotal();
        //Integer转换为Long,都是引用数据类型，不能直接转换（小转大）
        return new PageTemplate<Student>(students,total.longValue());
    }

    @Override
    public PageTemplate<Student> getPageStudent(Integer pageNum, Integer pageSize) {
        //参数校验
        if(Objects.isNull(pageNum) || Objects.isNull(pageSize)){
            throw new SystemException(ResponseEnum.PAGENUM_OR_PAGESIZE_IS_NUL);
        }
        //1.开启分页
        PageHelper.startPage(pageNum,pageSize);
        //2.核心业务查询（查询所有学生）
        List<Student> students=studentMapper.selectAllStudent();
        //3.封装PageInfo对象
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        //todo 封装PageTemplate对象      1.long->int强制转换 2.修改Template,Integer改为Long
        System.out.println("studentPageInfo = " + studentPageInfo);
        return new PageTemplate<Student>(studentPageInfo.getList(),studentPageInfo.getTotal());


    }

    @Override
    public PageTemplate<Student> getPageStudentByCondition(Integer pageSize, Integer pageNum, Integer id, String name, Integer gender, LocalDateTime startTime, LocalDateTime endTime) {
        //参数校验
        if(Objects.isNull(pageNum) || Objects.isNull(pageSize)){
            throw new SystemException(ResponseEnum.PAGENUM_OR_PAGESIZE_IS_NUL);
        }
        if (Objects.nonNull(gender) && gender != 1 && gender != 0){
            throw new SystemException(ResponseEnum.GENDER_FORMAT_ERROR);
        }
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //核心业务查询
        List<Student> students = studentMapper.getStudentByCondition(id,name,gender,startTime,endTime);
        //封装PageInfo对象
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        //返回分页模版数据
        return new PageTemplate<Student>(studentPageInfo.getList(),studentPageInfo.getTotal());


    }


}
